<?php
declare (strict_types = 1);
namespace app\promotion\model;

use app\common\model\BaseModel;
use app\user\logic\user\Account;
use think\model\concern\SoftDelete;

/**
 * 团购活动模型
 */
class PromotionTuan extends BaseModel
{
    use softDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'promotion_tuan';

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'            => 'integer',
        'promotion_id'  => 'integer',
        'user_id'       => 'integer',
        'total_num'     => 'integer',
        'join_num'      => 'integer',
        'create_time'   => 'integer',
        'expiry_time'   => 'integer',
        'status'        => 'integer',
        'refund'        => 'integer',
    ];

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    protected $append = ['expiry_countdown'];

    /**
     * 开团状态查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-14
     * @param  [type]  $query  [description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function scopeStatus($query, $status = 1)
    {
        $query->whereStatus($status);
    }

    /**
     * 活动条件查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-08
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopePromotionId($query, $promotion_id = 0)
    {
        $query->wherePromotionId($promotion_id);
    }

    /**
     * 活动条件查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-08
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeUserId($query, $user_id = 0)
    {
        $user_id = $user_id ? : request()->mid;
        $query->whereUserId($user_id);
    }

    /**
     * 获取团购状态
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-15
     * @param  string $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function getStatusAttr($value='', $data)
    {
        return ($data['expiry_time'] < time() && $data['join_num'] < $data['total_num']) ? 3 : $value;
    }

    /**
     * 获取团购状态
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-15
     * @param  string $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function getExpiryCountdownAttr($value='', $data)
    {
        return max(0, $data['expiry_time'] - time() );
    }

    /**
     * 获取入团状态，成功入返回数量。使用时请使用withCount
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-15
     * @return [type] [description]
     */
    public function joinStatus()
    {
        return $this->hasOne(PromotionTuanData::class, 'tuan_id')->userId()->status();
    }

    /**
     * 关联参团数据表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-14
     * @return [type] [description]
     */
    public function tuanData()
    {
        return $this->hasMany(PromotionTuanData::class, 'tuan_id');
    }

    /**
     * 关联活动信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-14
     * @return [type] [description]
     */
    public function promotion()
    {
        return $this->belongsTo(Promotion::class);
    }

    /**
     * 用户头像名称关联
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-09
     * @param string $value [description]
     */
    public function userAvatarName()
    {
        return $this->belongsTo(Account::class, 'user_id')->with(['avatar_url'])->bind(['current_user_name' => 'nick_name', 'current_user_avatar_url' => 'avatar_url']);
    }
}
